package com.swt.draw.controller;

import com.alibaba.fastjson.JSONObject;
import com.swt.draw.dao.AdminMapper;
import com.swt.draw.entity.Admin;
import com.swt.draw.enums.LoginEnum;
import com.swt.draw.service.AdminService;
import com.swt.draw.vo.BaseResponse;
import com.swt.draw.vo.LoginVO;
import com.swt.draw.vo.RegistVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class AccountController {

    @Resource
    private AdminService adminService;

    /**
     * 登录
     * @return
     */
    @PostMapping("/doLogin")
    public BaseResponse doLogin(@RequestBody LoginVO loginVO, HttpServletRequest request){
        BaseResponse response = new BaseResponse();
        response.setCode(LoginEnum.LOGIN_FAIL.getCode());
        response.setDesc(LoginEnum.LOGIN_FAIL.getDesc());
        if(loginVO != null){
            try {
                UsernamePasswordToken token = new UsernamePasswordToken(loginVO.getUsername(), loginVO.getPassword());
                Subject subject = SecurityUtils.getSubject();
                subject.login(token);
                // 登录成功
                request.getSession().setAttribute("adminNo", ((Admin)subject.getPrincipal()).getAdminNo());
                response.setCode(LoginEnum.LOGIN_SUCCESS.getCode());
                response.setDesc(LoginEnum.LOGIN_SUCCESS.getDesc());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return response;
    }

    /**
     * 注册
     * @return
     */
    @PostMapping("/doRegist")
    public BaseResponse doRegist(@RequestBody RegistVO registVO){
        BaseResponse response = new BaseResponse();
        System.out.println(registVO);
        if(registVO != null && StringUtils.isNotEmpty(registVO.getPassword()) && StringUtils.isNotEmpty(registVO.getUsername())){
            if(registVO.getPassword().equals(registVO.getPwdConfirm())){
                Admin admin = new Admin();
                admin.setAdminLoginName(registVO.getUsername());
                // 检查是否用户名冲突
                List<Admin> admins = adminService.getAdminByLoginName(registVO.getUsername());
                if(admins != null && admins.size() > 0){
                    response.setDesc("账号已存在");
                    return response;
                }

                String adminNo = "admin-" + UUID.randomUUID().toString();
                admin.setAdminNo(adminNo);
                admin.setAdminCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                admin.setAdminPwd(new Md5Hash(registVO.getPassword(), registVO.getUsername(), 2).toString());
                admin.setAdminStatus((short) 0);
                admin.setAdminSex("unknow");
                adminService.addAdmin(admin);
                response.setCode(1001);
                response.setDesc("success");
            }
        }
        return response;
    }

}
